package cn.yeziji.forum.service.impl;

import cn.yeziji.forum.dao.intf.ForumAnswerDao;
import cn.yeziji.forum.dao.intf.ForumDetailsDao;
import cn.yeziji.forum.dao.intf.ForumQuestionDao;
import cn.yeziji.forum.dao.intf.ForumUserDao;
import cn.yeziji.forum.entity.ForumUserEntity;
import cn.yeziji.forum.exception.UserException;
import cn.yeziji.forum.service.StaticsService;
import cn.yeziji.forum.common.enums.status.UserStatus;
import cn.yeziji.forum.utils.JwtOperaUtils;
import cn.yeziji.forum.vo.user.UserStaticsDetailVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 统计服务实现逻辑层
 *
 * @author gzkemays
 * @since 2022/11/26 8:22 PM
 */
@Service
public class StaticsServiceImpl implements StaticsService {
  // 统计对象
  @Resource ForumDetailsDao detailsDao;
  @Resource ForumAnswerDao answerDao;
  @Resource ForumQuestionDao questionDao;
  @Resource ForumUserDao userDao;
  @Resource HttpServletRequest request;

  @Override
  public UserStaticsDetailVO statics7DaysList(String nickname) {
    Integer userId = 0;
    ForumUserEntity userEntity;
    if (nickname != null) {
      userEntity = userDao.selectUserByNickName(nickname);
      userId = userEntity.getId();
    } else {
      userEntity = userDao.selectById(userId);
      userId = JwtOperaUtils.getUserIdByRequest(request);
    }
    if (userEntity == null || userId == 0) throw new UserException(UserStatus.USER_IS_NOT_FOUND);
    // 开启统计
    return new UserStaticsDetailVO(
        detailsDao.getUser7DaysDetails(userId),
        questionDao.getUser7DaysQuestions(userId),
        answerDao.getUser7DaysAnswer(userId));
  }
}
